package crew.gwt.client.widgets;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DecoratedPopupPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

import crew.gwt.client.widgets.handler.DialogHandler;

public class DialogPopupPanel extends DecoratedPopupPanel implements ClickHandler {

	private VerticalPanel vPanel = new VerticalPanel();
	private Button btClose = new Button("Annuler");
	private Button btOK = new Button("Confirmer");
	
	private final DialogHandler handler;
	
	public DialogPopupPanel(Widget content,DialogHandler handler) {
		super(false, true);
		this.handler = handler;
		setGlassEnabled(true);
		setAnimationEnabled(true);
		btClose.addClickHandler(this);
		btOK.addClickHandler(this);

		vPanel.add(new HTML("<br /> "));
		vPanel.add(content);
		vPanel.add(new HTML("<br /> <hr /> "));

		HorizontalPanel hTemp = new HorizontalPanel();
		hTemp.setSpacing(5);
		hTemp.add(btClose);
		hTemp.add(btOK);
		vPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		vPanel.add(hTemp);
		super.add(vPanel);

		center();
	}
	
	public DialogPopupPanel(Widget content,DialogHandler handler,String textBtConfirm) {
		this(content, handler);
		this.btOK.setText(textBtConfirm);
	}

	@Override
	public void onClick(ClickEvent event) {
		hide();
		if ( handler != null && event.getSource().equals(btClose)) {
			handler.onCancelClick();
		} else if( handler != null ) {
			handler.onConfirmClick();
		}
	}
	
//	protected void setContent(Widget content){
//		vPanel.clear();
//		vPanel.add(new HTML("<br /> "));
//		vPanel.add(content);
//		vPanel.add(new HTML("<br /> <hr /> "));
//	}
	
	@Override
	public void add(Widget w) {
		vPanel.add(w);
	}

	public Button getBtClose() {
		return btClose;
	}

	public Button getBtOK() {
		return btOK;
	}
}
